package com.marktim.pokerpoker.menu;

import org.jraf.android.backport.switchwidget.Switch;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.marktim.pokerpoker.R;
import com.marktim.pokerpoker.util.Font;
import com.marktim.pokerpoker.util.GameSettings;

public class MenuSectionFragment extends Fragment {
	public static final String ARG_SECTION_NUMBER = "section_number";
	public static final int PAGE_CREATE = 0;
	public static final int PAGE_JOIN = 1;
	public static final int PAGE_SETTINGS = 2;

	private static final int MAX_PLAYERS_DEFAULT = 8;

	private static final int[][] CHIP_VALUE_DATA = new int[][] {
			new int[] {
					1, 2, 5, 10, 20
			}, new int[] {
					1, 5, 25, 50, 100
			}, new int[] {
					10, 20, 50, 100, 500
			}
	};

	private static final int[][] CHIP_COUNT_DATA = new int[][] {
			new int[] {
					15, 10, 5, 7, 2
			}, new int[] {
					40, 40, 20, 10, 5
			}
	};

	private GameSettings gs;

	public MenuSectionFragment() {
		gs = GameSettings.getInstance();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_menu, container, false);

		int page = getArguments().getInt(ARG_SECTION_NUMBER);

		LinearLayout content = (LinearLayout) rootView.findViewById(R.id.menuFragmentContent);

		View view;
		TextView title = (TextView) rootView.findViewById(R.id.menuTitle);
		title.setTypeface(Font.getRobotoThin(getActivity()));

		switch (page) {
		case PAGE_CREATE:
			view = inflater.inflate(R.layout.menu_create, null);
			title.setText(R.string.createGame);

			final TextView[] titles = new TextView[] {
					(TextView) view.findViewById(R.id.createTableNameText),
					(TextView) view.findViewById(R.id.createMaxPlayersText),
					(TextView) view.findViewById(R.id.createChipValueText),
					(TextView) view.findViewById(R.id.createChipCountText),
					(TextView) view.findViewById(R.id.createSmallBlindText),
					(TextView) view.findViewById(R.id.createBigBlindText),
					(TextView) view.findViewById(R.id.createIncreaseBlindsText)
			};

			for (TextView t : titles) {
				t.setTypeface(Font.getRobotoThin(getActivity()));
			}

			final EditText tableName = (EditText) view.findViewById(R.id.createTableNameInput);
			final SeekBar maxPlayers = (SeekBar) view.findViewById(R.id.createMaxPlayersInput);
			final TextView maxPlayersValue = (TextView) view.findViewById(R.id.createMaxPlayersValue);
			final Spinner chipValue = (Spinner) view.findViewById(R.id.createChipValueInput);
			final Spinner chipCount = (Spinner) view.findViewById(R.id.createChipCountInput);
			final SeekBar smallBlind = (SeekBar) view.findViewById(R.id.createSmallBlindInput);
			final TextView smallBlindValue = (TextView) view.findViewById(R.id.createSmallBlindValue);
			final SeekBar bigBlind = (SeekBar) view.findViewById(R.id.createBigBlindInput);
			final TextView bigBlindValue = (TextView) view.findViewById(R.id.createBigBlindValue);
			final Switch increaseBlinds = (Switch) view.findViewById(R.id.createIncreaseBlindsInput);

			// TABLE NAME
			tableName.setTypeface(Font.getRobotoRegular(getActivity()));
			tableName.addTextChangedListener(new TextWatcher() {

				@Override
				public void afterTextChanged(Editable text) {
					gs.setName(text.toString());
				}

				@Override
				public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				}

				@Override
				public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				}
			});

			// MAX PLAYERS
			maxPlayersValue.setTypeface(Font.getRobotoThin(getActivity()));
			maxPlayers.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					gs.setMaxPlayers(seekBar.getProgress() + 2);
				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					int players = progress + 2;
					maxPlayersValue.setText(Integer.toString(players));
				}
			});
			maxPlayers.setProgress(MAX_PLAYERS_DEFAULT - 2);

			// CHIP VALUE
			final SpinnerAdapter chipValueAdapter = new MenuChipsAdapter(getActivity(), CHIP_VALUE_DATA);
			chipValue.setAdapter(chipValueAdapter);
			chipValue.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
					gs.setChipValues((int[]) chipValueAdapter.getItem(position));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
			
			// CHIP COUNT
			final SpinnerAdapter chipCountAdapter = new MenuChipsAdapter(getActivity(), CHIP_COUNT_DATA);
			chipCount.setAdapter(chipCountAdapter);
			chipCount.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
					gs.setChipCount((int[]) chipCountAdapter.getItem(position));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
			
			// SMALL BLIND
			smallBlindValue.setTypeface(Font.getRobotoThin(getActivity()));
			smallBlind.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					int blind = GameSettings.calculateSmallBlind(seekBar.getProgress());
					gs.setSmallBlind(blind);
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					int blind = GameSettings.calculateSmallBlind(progress);
					smallBlindValue.setText(Integer.toString(blind));
				}
			});
			smallBlind.setProgress(1);

			// BIG BLIND
			bigBlindValue.setTypeface(Font.getRobotoThin(getActivity()));
			bigBlind.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					int blind = GameSettings.calculateBigBlind(seekBar.getProgress());
					gs.setBigBlind(blind);
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					int blind = GameSettings.calculateBigBlind(progress);
					bigBlindValue.setText(Integer.toString(blind));
				}
			});
			bigBlind.setProgress(1);
			
			// INCREASE BLINDS
			increaseBlinds.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean checked) {
					gs.setIncreaseBlinds(checked);
				}
			});

			content.addView(view);
			break;
		case PAGE_JOIN:
			view = inflater.inflate(R.layout.menu_join, null);
			title.setText(R.string.joinGame);
			content.addView(view);
			break;
		case PAGE_SETTINGS:
			view = inflater.inflate(R.layout.menu_settings, null);
			title.setText(R.string.settings);
			content.addView(view);
			break;
		}

		return rootView;
	}
}